;******************************************************************
; Input/Output 16 bits functions (real mode)
; Reindeer OS - boot loader
;
; Copyrights 2011 Michal Saman, m.saman@designplus.cz.
; This source code is release under the Apache License 2.0.
; http://www.apache.org/licenses/LICENSE-2.0
; *****************************************************************

;---------------------------------------
; 16 Bits functions throug BIOS int
;---------------------------------------
bits 16

;***************************************
; Puts16()
; Print a null terminated string
; Params:
;	DS:SI String terminated string by 0
;***************************************

Puts16:
	pusha						; save all universal registers
	pushf						; save flags
.Puts16Loop:
	lodsb						; load one byte from SI to AL
	or al, al					; is AL=0?
	jz .Puts16Done				; yes, gone
	mov	ah, 0eh
	int 10h						; invoke BIOS print function
	jmp	.Puts16Loop
.Puts16Done:
	popf						; restore flags
	popa						; restore registers
	ret							; return to main code sequence
	
;************************************************
; NumToAscii
; prevede cislo na ASCII kod
; Params:
; ax = 16 bit cislo
; bx = zaklad soustavy cisla
; di = pointer na bufer kam zapsat vysledek
; Returns
; Buffer with ASCII string derived form number
;***********************************************

NumToAscii:

pusha					; uloz registry
pushf					; save flags
xor si, si

jmp prevod

hexdigit:
	cmp dl, 10
	jb .mensi
	add dl, 'A'-10
	ret
	.mensi:
		or dl, '0'
		ret

prevod:
	xor dx, dx
	div bx				; AX / BX = AX, zbytek do DX
	call hexdigit
	push dx
	inc si
	test ax, ax
	jnz prevod
	cld

zapis:
	pop ax				; nactu znak ze zasobniku
	stosb				; ulozim ho na pozici es:di
	dec si				; snizim registr si
	test si, si			; pokuj je si == 0 je zasobnik prazdny
	jnz zapis			; dokud neni nula, zapisuji dal
	
mov ax, 0
stosb					; ukoncim retezec nulou

popf					; restore flags
popa					; restpre registers
ret

;******************************************
; Print register
; Params:
;	ax = value of register to print
;******************************************
PrintRegister16:
	pusha								; save all register
	push	ds							; save entry DS
	push	es							; save entry ES

	mov		bx, PRG_CODE_SEGMENT		; return to our default segment
	mov		ds, bx
	mov		es, bx
	mov		di, [numberBuffer]
	mov		bx, 2
	call	NumToAscii
	mov		si, [numberBuffer]
	call	Puts16
	mov		si, msgCrLf
	call	Puts16
	
	pop		es							; restore ES
	pop		ds							; restore DS
	popa								; restore registers
	ret
